const router = require("koa-router")();
const Mock = require("mockjs");
const JWT = require("jsonwebtoken");
let { list } = Mock.mock({
  "list|20": [
    {
      "img|+1": [
        "https://img95.699pic.com/xsj/12/ps/47.jpg%21/fw/700/watermark/url/L3hzai93YXRlcl9kZXRhaWwyLnBuZw/align/southeast",
        "https://img95.699pic.com/xsj/12/k9/2t.jpg%21/fw/700/watermark/url/L3hzai93YXRlcl9kZXRhaWwyLnBuZw/align/southeast",
        "https://img1.baidu.com/it/u=292796048,365997646&fm=253&fmt=auto&app=138&f=JPEG?w=750&h=500",
        "https://img95.699pic.com/xsj/12/pu/dv.jpg%21/fh/300",
        "https://img95.699pic.com/xsj/0s/u1/pd.jpg%21/fh/300",
        "https://pic.quanjing.com/8u/fz/QJ7100338337.jpg@%21350h",
        "https://img0.baidu.com/it/u=229984261,1146828493&fm=253&fmt=auto&app=138&f=JPEG?w=755&h=500",
        "https://img95.699pic.com/xsj/0z/a2/zr.jpg%21/fh/300",
        "https://file.moyublog.com/d/file/2020-11-28/7c77407b0a3f44468693fe899cef8aef.jpg",
        "https://img0.baidu.com/it/u=3019306725,899400849&fm=253&fmt=auto&app=138&f=JPEG?w=700&h=394",
        "https://img0.baidu.com/it/u=4017706871,3591834496&fm=253&fmt=auto&app=138&f=JPEG?w=640&h=487",
        "https://img0.baidu.com/it/u=2683734960,2798949578&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=333",
        "https://img1.baidu.com/it/u=1833885039,2850168574&fm=253&fmt=auto&app=138&f=JPEG?w=700&h=465",
        "https://img1.baidu.com/it/u=3332181097,803381709&fm=253&fmt=auto&app=138&f=JPEG?w=749&h=500",
        "https://img95.699pic.com/xsj/2h/za/bs.jpg%21/fh/300",
        "https://pic3.zhimg.com/0552bfe4353745f4036a0b2641ed9882_r.jpg",
        "https://img1.baidu.com/it/u=3639954091,194618632&fm=253&fmt=auto&app=138&f=JPEG?w=480&h=306",
        "https://img95.699pic.com/xsj/2i/c7/vg.jpg%21/fh/300",
        "https://img95.699pic.com/xsj/2g/xc/7x.jpg%21/fh/300",
        "https://img.saihuitong.com/2587/richtext/92898/1621d31727c.jpg-w1030",
      ],
      title: "@ctitle(2)",
      "price|100-300": 1,
      word: "@cword(8)",
      id: "@id",
      "hbq|+1": ["火爆区", "正常区", "低平区"],
    },
  ],
});

router.get("/search", (ctx) => {
  let { value } = ctx.request.query;

  let items = list.filter((item) => item.title.includes(value));

  if (items) {
    ctx.body = {
      code: 200,
      items: items,
    };
  } else {
    ctx.body = {
      items: list,
    };
  }
});

router.get("/screen", (ctx) => {
  let { value0 } = ctx.request.query;

  if (value0 == 0) {
    ctx.body = {
      items: list,
    };
  } else if (value0 == 1) {
    let items = list.filter((item) => item.hbq == "火爆区");

    ctx.body = {
      items: items,
    };
  } else if (value0 == 2) {
    let items = list.filter((item) => item.hbq == "正常区");

    ctx.body = {
      items: items,
    };
  } else if (value0 == 3) {
    let items = list.filter((item) => item.hbq == "低平区");

    ctx.body = {
      items: items,
    };
  }
});

router.get("/hblist", (ctx) => {
  ctx.body = {
    code: 200,
    list,
  };
});

router.get("/list", (ctx) => {
  ctx.body = {
    code: 200,
    list,
  };
});

router.get("/detail", (ctx) => {
  let { id } = ctx.request.query;
  let items = list.find((item) => item.id == id);
  if (items) {
    ctx.body = {
      code: 200,
      msg: "数据获取成功",
      items: items,
    };
  } else {
    ctx.body = {
      code: 400,
      msg: "数据获取失败",
    };
  }
});

let user = [
  {
    username: "admin",
    password: "admin",
    info: "https://t7.baidu.com/it/u=2609096218,1652764947&fm=193&f=GIF",
  },
  {
    username: "user",
    password: "user",
    info: "https://t7.baidu.com/it/u=240414225,3094575204&fm=193&f=GIF",
  },
];
router.post("/sigin", (ctx) => {
  let { username, password } = ctx.request.body;
  let items = user.find((item) => item.username == username);
  if (items) {
    ctx.body = {
      code: 400,
      msg: "注册失败",
    };
  } else {
    user.push({
      ...ctx.request.body,
      info: "https://t7.baidu.com/it/u=3487709764,387491787&fm=193&f=GIF",
    });
    ctx.body = {
      code: 200,
      msg: "注册成功",
    };
  }
});
router.post("/login", (ctx) => {
  let { username, password } = ctx.request.body;
  let items = user.find((item) => item.username == username);
  if (items) {
    ctx.body = {
      code: 200,
      msg: "登录成功",
      user: items.username,
      info: items.info,
      token: JWT.sign(items.username, "werfgrfe"),
    };
  } else {
    ctx.body = {
      code: 400,
      msg: "登录失败",
    };
  }
});

module.exports = router;
